package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.windoent.yugioh.R;
import com.windoent.yugioh.bean.PanFaStyleBean;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.EditLimitStringUtils;
import com.windoent.yugioh.utils.SharedPreUtil;
import com.windoent.yugioh.utils.TDevice;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.popupwindow.PanFaPopUpWindow;
import com.youth.banner.util.BannerUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.util.List;

import okhttp3.Call;

/**
 * 作者：Android
 * 时间：2022/1/16 11:08
 * 功能：繼承Dialog，自定义更新Dialog
 */
public class PanFaUserDetailsDialog extends Dialog {
    private Context mContext;
    private LinearLayout mLlRecordMore;
    private TextView mTvNo;
    private TextView mTvYes;
    private TextView mTvName;
    private RelativeLayout mRlSelect;
    private EditText mEtContent;
    private TextView mTvType;


    private onClickListener listener;
    private String nameStr, gameId, userId, panFaStyleStr;
    private List<PanFaStyleBean.DataBeanX.DataBean> data;
    private int type_id = 0;

    public void setOnclickListener(onClickListener clickListener) {
        this.listener = clickListener;
    }

    public PanFaUserDetailsDialog(Context context) {
        super(context, R.style.MoHUDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_no_title_panfa_user_details_dialog_layout);
        //按空白处能取消动画
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        mTvYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    if (type_id == 0) {
                        ToastUtils.showText(getContext().getString(R.string.string_select_panfa_style));
                        return;
                    }
                    listener.onClickSure(type_id + "", mEtContent.getText().toString());
                }
                dismiss();
            }
        });
        mTvNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mLlRecordMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.clickMore();
                }
            }
        });
        mRlSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (data == null) {
                    ToastUtils.showText(getContext().getString(R.string.string_no_data_panfa_style));
                    return;
                }
                showTyoePop();
            }
        });
        EditLimitStringUtils.lengthFilter(mEtContent, 60, mContext.getString(R.string.string_panfa_et_hint));
    }

    //分类
    private void showTyoePop() {
        new PanFaPopUpWindow(mContext, data, BannerUtils.dp2px(40), new PanFaPopUpWindow.OnInterface() {
            @Override
            public void onSelect(PanFaStyleBean.DataBeanX.DataBean bean) {
                mTvType.setText(bean.getName());
                type_id = bean.getId();
            }
        }).showAtBottom(mRlSelect, 0, 0);
    }


    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        if (BaseMethod.notNull(nameStr)) {
            mTvName.setText(nameStr);
        }
        if (BaseMethod.notNull(panFaStyleStr)) {
            details(panFaStyleStr);
        }
    }

    private void details(String response) {
        if (BaseUtils.isJSONValid(response)) {
            try {
                if (BaseUtils.codeIs0(mContext, response)) {
                    Gson gson = new Gson();
                    PanFaStyleBean json = gson.fromJson(response, PanFaStyleBean.class);
                    if (json != null) {
                        PanFaStyleBean.DataBeanX datax = json.getData();
                        data = datax.getData();
                        if (data != null && data.size() > 0) {
                            PanFaStyleBean.DataBeanX.DataBean dataBean = data.get(0);
                            if (dataBean != null) {
                                mTvType.setText(dataBean.getName());
                                type_id = dataBean.getId();
                            }
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            ToastUtils.showText(getContext().getString(R.string.string_response_error));
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        mLlRecordMore = (LinearLayout) findViewById(R.id.ll_record_more);
        mTvNo = (TextView) findViewById(R.id.tv_no);
        mTvYes = (TextView) findViewById(R.id.tv_yes);
        mTvName = (TextView) findViewById(R.id.tv_name);
        mRlSelect = (RelativeLayout) findViewById(R.id.rl_select);
        mTvType = (TextView) findViewById(R.id.tv_type);
        mEtContent = (EditText) findViewById(R.id.et_content);

    }


    public interface onClickListener {

        public void onClickSure(String typeId, String etString);

        public void clickMore();
    }

    //设置用户信息
    public void setUserStr(String name, String gameId, String userId, String panFaStyleStr) {
        this.nameStr = name;
        this.gameId = gameId;
        this.userId = userId;
        this.panFaStyleStr = panFaStyleStr;
    }
}
